package nashorn;

import jdk.nashorn.api.scripting.NashornScriptEngine;
import stream.Person;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * 类名：MainTest
 * 开发人员: CoderJu
 * 创建时间: 2019/3/9 20:37
 * 描述:
 * 版本：V1.0
 */
public class MainTest {
    public static void main(String[] args) throws FileNotFoundException, ScriptException, NoSuchMethodException {
       /* ScriptEngineManager  scriptEngineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
        try {
            scriptEngine.eval("print('Hello word !')");
        } catch (ScriptException e) {
            e.printStackTrace();
        }*/
        readJs();
    }
    public static void readJs() throws FileNotFoundException, ScriptException, NoSuchMethodException {
        ScriptEngineManager  scriptEngineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
        scriptEngine.eval(new FileReader("G:\\ideaWorkspace\\JAVA_New\\src\\nashorn\\Script.js"));
        Invocable invocable = (Invocable) scriptEngine;
        Object result = invocable.invokeFunction("fun1", "Peter Parker");
        System.out.println(result);
        System.out.println(result.getClass());
        result = invocable.invokeFunction("fun2", new Person(1,"张三",1000));
        System.out.println(result);
    }

    public static int cal(int x,int y){
        System.out.println("---------cal");
        return x+y;
    }

}
